package com.zito.health.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.dto.LaboratoryChangeDto;
import com.zito.health.dto.LaboratoryChangePrintDto;
import com.zito.health.dto.LaboratoryPrint1Dto;
import com.zito.health.entity.Laboratory;
import com.zito.health.entity.LaboratoryChange;
import com.zito.health.entity.User;
import com.zito.health.service.LaboratoryChangeService;
import com.zito.health.util.CacheUtil;
import com.zito.health.vo.LaboratoryChangeVo;
import com.zito.health.vo.LaboratoryInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @description: 实验室变更
 * @author: Sam
 * @time: 2020/02/15 15:30
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping("/laboratoryChange")
public class LaboratoryChangeController {

    @Autowired
    private LaboratoryChangeService laboratoryChangeService;

    /**
     * 实验室变更添加
     * @param laboratoryChange
     * @return
     */
    @PostMapping("/saveBase")
    public VerifyResult saveBase(LaboratoryChange laboratoryChange){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryChange.setDeclareTime(new Date());
        laboratoryChangeService.save(laboratoryChange);
        return verifyResult;
    }

    /**
     * 实验室基础信息
     * @param laboratoryChange
     * @return
     */
    @PutMapping
    public VerifyResult update(LaboratoryChange laboratoryChange){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryChangeService.update(laboratoryChange);
        return verifyResult;
    }

    /**
     * 变更列表
     * @param laboratoryChangeDto
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping
    public PageResult list(LaboratoryChangeDto laboratoryChangeDto, @RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize){
        User user = CacheUtil.getCacheUser().getUser();
        if(user.getRoleId()==1){//1.admin
        }else if(user.getRoleId()==2) {//2市局
            laboratoryChangeDto.setStatesStr("2");
        }else if(user.getRoleId()==3){//县局
            laboratoryChangeDto.setOldAreaId(user.getAreaId());
            laboratoryChangeDto.setStatesStr("1,3,5");
        }else if(user.getRoleId()==9){//实验室单位管理员
            laboratoryChangeDto.setUnitId(user.getUnitId());//单位id
            laboratoryChangeDto.setStatesStr("1,2,3,5,6");//审核状态：1.未审核，2.县/区审核已通过，3.县/区审核未通过，4.市审核已通，5.市审核未通过，6.未提交
        }
        PageHelper.startPage(pageNo,pageSize);
        List<LaboratoryChangeDto> list = laboratoryChangeService.queryLaboratoryChange(laboratoryChangeDto);
        PageInfo<LaboratoryChangeDto> pageInfo = new PageInfo(list);
        PageResult<LaboratoryChangeDto> result = new PageResult();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    /**
     * 变更列表
     * @param laboratoryChangeDto
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/4LabList")
    public PageResult forLabList(LaboratoryChangeDto laboratoryChangeDto, @RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize){
        PageHelper.startPage(pageNo,pageSize);
        List<LaboratoryChangeDto> list = laboratoryChangeService.queryLaboratoryChange(laboratoryChangeDto);
        PageInfo<LaboratoryChangeDto> pageInfo = new PageInfo(list);
        PageResult<LaboratoryChangeDto> result = new PageResult();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @GetMapping("/getLabInfoByLabId/{laboratoryId}")
    public LaboratoryInfoVo getLabInfoByLabId(@PathVariable Integer laboratoryId){
        LaboratoryInfoVo laboratoryInfoVo = laboratoryChangeService.getLabInfoByLabId(laboratoryId);
        return laboratoryInfoVo;
    }

    @GetMapping("/getLabInfoByLabChangeId/{changeId}")
    public LaboratoryChangeVo getLabInfoByLabChangeId(@PathVariable Integer changeId){
        LaboratoryChangeVo laboratoryChangeVo = laboratoryChangeService.getLabInfoByLabChangeId(changeId);
        return laboratoryChangeVo;
    }

    /**
     * 实验室变更删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}/{laboratoryId}")
    public VerifyResult delete(@PathVariable Integer id,@PathVariable Integer laboratoryId){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryChangeService.deleteById(id,laboratoryId);
        return verifyResult;
    }

    @GetMapping("/print/{changId}")
    public LaboratoryChangePrintDto print(@PathVariable Integer changId){
        LaboratoryChangePrintDto laboratoryChangePrintDto = new LaboratoryChangePrintDto();
        if (changId != null) {
            laboratoryChangePrintDto = laboratoryChangeService.print(changId);
        }
        return laboratoryChangePrintDto;
    }

}
